package com.mtk.launcher.power.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cdl.library.adapter.BaseQuickAdapter;
import com.cdl.library.adapter.BaseViewHolder;
import com.cdl.library.adapter.QuickAdapter;
import com.cdl.library.adapter.listener.OnItemChildClickListener;
import com.cdl.library.dialog.OridinryDialog;
import com.cdl.library.listener.OridinryDialogClick;
import com.mtk.launcher.R;
import com.mtk.launcher.activity.BaseActivity;
import com.mtk.launcher.databinding.ActivityPowerOnOffBinding;
import com.mtk.launcher.power.bean.TimeDealEntity;
import com.mtk.launcher.power.bean.TimeLocalEntity;
import com.mtk.launcher.power.util.PowerOnOffUtil;
import com.mtk.launcher.power.view.WeekView;
import com.mtk.launcher.util.SimpleDateUtil;
import com.mtk.launcher.util.shared.SharedPerManager;

import java.util.ArrayList;
import java.util.List;

public class PowerManagerActivity extends BaseActivity {

    ActivityPowerOnOffBinding binding;
    PowerOnOffUtil powerOnOffUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPowerOnOffBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        initPowerOnOff();
        initListener();
    }

    private void initView() {
        binding.included.title.setText(getString(R.string.switcher_timing));
        powerOnOffUtil = new PowerOnOffUtil(PowerManagerActivity.this);
    }

    private void initListener() {
        binding.included.llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.btnAddTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PowerManagerActivity.this, TimerChangeActivity.class));
            }
        });
        binding.btnClearTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClearAllDataDialog();
            }
        });

        binding.btnLogcat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PowerManagerActivity.this, PowerInOffLogActivity.class));
            }
        });
    }


    QuickAdapter adapterPower;
    List<TimeLocalEntity> timerDbEntityList = new ArrayList<>();

    private void initPowerOnOff() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PowerManagerActivity.this);
        binding.recyclePower.setLayoutManager(linearLayoutManager);
        adapterPower = new QuickAdapter<TimeLocalEntity>(R.layout.item_power_info) {
            @Override
            protected void convert(BaseViewHolder viewHolder, TimeLocalEntity controlEnity,int position) {
                viewHolder.setText(R.id.tv_time_on, controlEnity.getTtOnTime());
                viewHolder.setText(R.id.tv_time_off, controlEnity.getTtOffTime());
                WeekView weekly_layout = viewHolder.getView(R.id.week_view);
                weekly_layout.updateViewStatus(controlEnity.getTtMon().equals("true"),
                        controlEnity.getTtTue().equals("true"),
                        controlEnity.getTtWed().equals("true"),
                        controlEnity.getTtThu().equals("true"),
                        controlEnity.getTtFri().equals("true"),
                        controlEnity.getTtSat().equals("true"),
                        controlEnity.getTtSun().equals("true")
                );
            }
        };
        binding.recyclePower.setAdapter(adapterPower);
        adapterPower.addChildClickViewId(R.id.rela_delete);
        adapterPower.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter<?, ?> baseAdapter, View view, int position) {
                TimeLocalEntity timeLocalEntity = (TimeLocalEntity) baseAdapter.getItem(position);
                showDelItemDialog(timeLocalEntity);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        updateSqlListInfo();
    }

    private void updateSqlListInfo() {
        timerDbEntityList.clear();
        timerDbEntityList = powerOnOffUtil.queryTimerList();
        if (timerDbEntityList == null || timerDbEntityList.size() < 1) {
            SharedPerManager.setPowerOnOffTime(null, "界面刷新数据，清理");
        }

        adapterPower.setNewData(timerDbEntityList);
        TimeDealEntity timeDealEntity = SharedPerManager.getPowerOnOffTime();

        String offTime = "关机时间  " + SimpleDateUtil.getShowOnOffTime(this, timeDealEntity, 0);
        String onTime = "开机时间  " + SimpleDateUtil.getShowOnOffTime(this, timeDealEntity, 1);
        binding.tvShowTime.setText(offTime + "\n" + onTime);
    }

    private void showClearAllDataDialog() {
        OridinryDialog oridinryDialog = new OridinryDialog(PowerManagerActivity.this);
        oridinryDialog.setOnDialogClickListener(new OridinryDialogClick() {
            @Override
            public void sure() {
                powerOnOffUtil.clearAllPowerOnOff("用户手动清理定时数据");
                updateSqlListInfo();
            }

            @Override
            public void noSure() {

            }
        });
        oridinryDialog.show("删除", getString(R.string.clear_all_content));
    }

    private void showDelItemDialog(TimeLocalEntity timeLocalEntity) {
        OridinryDialog oridinryDialog = new OridinryDialog(PowerManagerActivity.this);
        oridinryDialog.setOnDialogClickListener(new OridinryDialogClick() {
            @Override
            public void sure() {
                boolean isDel = powerOnOffUtil.delTimerById(timeLocalEntity.getId());
                if (isDel) {
                    updateSqlListInfo();
                }
            }

            @Override
            public void noSure() {

            }
        });
        String showTime = timeLocalEntity.getTtOffTime() + " - " + timeLocalEntity.getTtOnTime();
        oridinryDialog.show("删除", "是否删除 " + showTime + "数据?");
    }

}
